package ch01
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import tools.Triangle;
	import tools.Triangle;
	
	/**
	 * 不规则物体碰撞测试(三角形)
	 * @author sjp
	 *
	 */
	public class BitmapCollision4 extends Sprite
	{
		private var bmpd1:BitmapData;
		private var bmp1:Bitmap;
		private var bmpd2:BitmapData;
		private var bmp2:Bitmap;
		
		public function BitmapCollision4()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			var radius:Number = 50.0;
			var diameter:Number = radius * 2;
			// make a star
			var tri:Triangle = new Triangle(radius);
			// make a fixed bitmap, draw the star into it
			bmpd1 = new BitmapData(diameter, diameter, true, 0);
			trace(tri.transform.matrix);
			bmpd1.draw(tri, new Matrix(1, 0, 0, 1, radius, radius));
			bmp1 = new Bitmap(bmpd1);
			bmp1.x = 200;
			bmp1.y = 200;
			addChild(bmp1);
			// make a moveable bitmap, draw the star into it, too
			bmpd2 = new BitmapData(diameter, diameter, true, 0);
			bmpd2.draw(tri, new Matrix(1, 0, 0, 1, radius, radius));
			bmp2 = new Bitmap(bmpd2);
			addChild(bmp2);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoving);
		}
		
		private function onMouseMoving(event:MouseEvent):void
		{
			// move bmp2 to the mouse position (centered).
			bmp2.x = mouseX - 50;
			bmp2.y = mouseY - 50;
			// the hit test itself.
			if (bmpd1.hitTest(new Point(bmp1.x, bmp1.y), 255, bmpd2, new Point(bmp2.x, bmp2.y), 255))
			{
				bmp1.filters = [new GlowFilter()];
				bmp2.filters = [new GlowFilter()];
			}
			else
			{
				bmp1.filters = [];
				bmp2.filters = [];
			}
		}
	}
}